<?php

//This function uploads an image and resizes it

function resizeImage($field, $desiredwidth, $thumbnailwidth,$directory,$maxSize,$thumbPrefix,$desiredname)
{

if ( (empty($field)) || (empty($desiredwidth)) || (empty($thumbnailwidth)) || (empty($directory)) || (empty($maxSize)) || (empty($thumbPrefix)) || (empty($desiredname)) )
	{
		return null;
	}


	$thumbsize = $thumbnailwidth; // the thumbnail width
	$desiredsize = $desiredwidth; //the desired width of the image

	$filedir = $directory.'/'; // the directory for the original image
	$thumbdir = $directory.'/'; // the directory for the thumbnail image
	$prefix = $thumbPrefix; // the prefix to be added to the original name
	
	$desiredname = $desiredname; //the desired name of the image
		
	$maxfile = $maxSize;
	$mode = '0666';
	
	$userfile_name = $_FILES[$field]['name'];
	$userfile_tmp = $_FILES[$field]['tmp_name'];
	$userfile_size = $_FILES[$field]['size'];
	$userfile_type = $_FILES[$field]['type'];
	
	if (isset($_FILES[$field]['name'])) 
	{
		//remove any spaces from the image name
		$prod_img = $filedir.ereg_replace("\s","",$userfile_name);

		$prod_img_thumb = $thumbdir.$prefix.$desiredname;
		$prod_img_scaled = $thumbdir.$desiredname;
		
		move_uploaded_file($userfile_tmp, $prod_img);
		chmod ($prod_img, octdec($mode));
		
		$sizes = getimagesize($prod_img);

		$aspect_ratio = $sizes[1]/$sizes[0]; 

		if ($sizes[0] <= $thumbsize)
		{
			$new_thumb_width = $sizes[0];
			$new_thumb_height = $sizes[1];
		}else{
			$new_thumb_width = $thumbsize;
			$new_thumb_height = abs($new_thumb_width*$aspect_ratio);
		}

		if ($sizes[0] <= $desiredsize)
		{
			$new_scale_width = $sizes[0];
			$new_scale_height = $sizes[1];
		}else{
			$new_scale_width = $desiredsize;
			$new_scale_height = abs($new_scale_width*$aspect_ratio);
		}

		$destthumbimg=imagecreatetruecolor($new_thumb_width,$new_thumb_height)
			or die('Problem In Creating Thumbnail image');

		$destscaleimg=imagecreatetruecolor($new_scale_width,$new_scale_height)
			or die('Problem In Creating Scaled image');			
			
		$srcimg=imagecreatefromjpeg($prod_img)
			or die('Problem In opening Source Image');
		if(function_exists('imagecopyresampled'))
		{
			imagecopyresampled($destthumbimg,$srcimg,0,0,0,0,$new_thumb_width,$new_thumb_height,ImageSX($srcimg),ImageSY($srcimg))
			or die('Problem In resizing thumbnail image');
			
			imagecopyresampled($destscaleimg,$srcimg,0,0,0,0,$new_scale_width,$new_scale_height,ImageSX($srcimg),ImageSY($srcimg))
			or die('Problem In resizing scaled image');			
			
		}else{
			imagecopyresized($destthumbimg,$srcimg,0,0,0,0,$new_thumb_width,$new_thumb_height,ImageSX($srcimg),ImageSY($srcimg))
			or die('Problem In resizing thumbnail image');

			imagecopyresized($destscaleimg,$srcimg,0,0,0,0,$new_scale_width,$new_scale_height,ImageSX($srcimg),ImageSY($srcimg))
			or die('Problem In resizing scaled image');			
			
		}
		imagejpeg($destthumbimg,$prod_img_thumb,90)
			or die('Problem In saving');

		imagejpeg($destscaleimg,$prod_img_scaled,90)
			or die('Problem In saving');			
			
		imagedestroy($destthumbimg);
		imagedestroy($destscaleimg);
		
		//deleting the orignal uploaded image
		unlink($prod_img);
		
		
		
	}
	
	return $prod_img_thumb;

}

//function checks for a valid e-mail address
//returns true if the e-mail address is valid and false if it is not valid
function check_email_address($email) {
// First, we check that there's one @ symbol, and that the lengths are right
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
return false;
}
}
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
return false;
}
}
}
return true;
}


function check_url ($url)
{
	if (!ereg("(https?://)?(([0-9a-z_!'().&=$%-]:)?[0-9a-z_!'().&=$%-]@)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-z_!'()-]\.)([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.[a-z]{2,6})(:[0-9]{1,4})?((/?)|(/[0-9a-z_!*'().;?:@&=$,%#-])/?)", $url))
	{
		return false;
	}
	else
	{
		return true;
	}
}

function is_valid_url($url) 
{   
	$pattern = "#^(http:\/\/|https:\/\/|www\.)(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?(\/)*$#i";   
	if (!preg_match($pattern, $url)) 
	{     
		return false;   
	} 
	else 
	{     
		return true;   
	} 
}


function removeElement($element,$array)
{
	if (  (empty($element)) || (empty($array))  )
	{
		return null;
	}
	
	$array2=$array;
	$match = '/'.$element.'/';
	for ($i=0;$i<count($array);$i++)
	{
		if (preg_match($match,$array[$i]))
		{
			array_splice($array2,$i,1);
		}
	}									
	//$qstring_result = implode("&",$array2);
	return 	$array2;
}

//returns an array containing the elements specified in an array
function getElementArray($element,$array)
{
	$retVal=array();
	if (  (empty($element)) || (empty($array))  )
	{
		return null;
	}
	
	$match = '/'.$element.'/';
	for ($i=0;$i<count($array);$i++)
	{
		if (preg_match($match,$array[$i]))
		{
			array_push($retVal,ereg_replace($element,"",$array[$i])); 
			
		}
	}									
	//$qstring_result = implode("&",$array2);
	return 	$retVal;
}

//This function displays any error messages that occurred
function displayGlobalErrorMessages()
{
        

            if (!empty($GLOBALS['message']))
            {
				echo '<div class="message">';
				echo '<p>An error has occurred!</p>';
                echo '<p>'.$GLOBALS['message'].'</p>';
				echo '</div>';
            }

            if (!empty($GLOBALS['successMessage']))
            {
				echo '<div class="successMessage">';
                echo '<p>'.$GLOBALS['successMessage'].'</p>';
				echo '</div>';
            }
}

?>